<template>
  <div>
    <!-- 面包屑导航组件 -->
    <bread-crumb :data="breadCrumbList"></bread-crumb>
    <el-card>
      <el-row
        :gutter="20"
        class="search-box"
      >
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="handleSearch"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="handleSearch"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button
            type="primary"
            @click="handleAddUser"
          >添加用户</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="userList"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column type="index"></el-table-column>

        <el-table-column
          prop="username"
          label="用户名"
        >
        </el-table-column>

        <el-table-column
          prop="email"
          label="邮箱"
        >
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="电话"
        >
        </el-table-column>
        <el-table-column
          prop="role_name"
          label="角色"
        >
        </el-table-column>
        <el-table-column label="状态">
          <template v-slot:default="{row}">
            <el-switch v-model="row.mg_state"></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180"
        >
          <template v-slot:default="{row}">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleEditUser(row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDeleteUser(row.id)"
            ></el-button>
            <el-tooltip
              effect="dark"
              content="分配权限"
              placement="top"
              :enterable="false"
            >
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-setting"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加/编辑用户对话框 -->
    <user-dialog
      :dialog-visible="dialogVisible"
      :user="userFormData"
      @close-dialog="handleCloseDialog"
      @refresh="getTableData"
    ></user-dialog>

  </div>
</template>

<script>
import UserDialog from "./components/UserDialog";

export default {
  data() {
    return {
      dialogVisible: false,
      userFormData: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },

      breadCrumbList: ["用户管理", "用户列表"],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      //保存请求回来的用户列表数据
      userList: [],
      total: 0,
    };
  },
  components: {
    UserDialog,
  },
  methods: {
    handleCloseDialog() {
      this.dialogVisible = false;
    },
    // 删除用户
    handleDeleteUser(userId) {
      this.$confirm("是否确定删除用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const { data: res } = await this.$http.delete(`users/${userId}`);

        this.$message[res.meta.status === 200 ? "success" : "error"](
          res.meta.msg
        );

        //
        if (this.userList.length <= 1) {
          this.queryInfo.pagenum =
            this.queryInfo.pagenum <= 1 ? 1 : this.queryInfo.pagenum - 1;
        }

        // 刷新用户列表
        this.getTableData();
      });
    },
    handleEditUser(row) {
      this.userFormData = row;
      this.dialogVisible = true;
    },

    // 添加用户
    handleAddUser() {
      // 显示对话框
      this.dialogVisible = true;
      this.userFormData = {
        username: "",
        password: "",
        email: "",
        mobile: "",
      };
    },
    handleSearch() {
      this.queryInfo.pagenum = 1;
      this.getTableData();
    },
    handleSizeChange(size) {
      this.queryInfo.pagesize = size;
      this.getTableData();
    },
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum;
      this.getTableData();
    },
    async getTableData() {
      const { data: res } = await this.$http.get("users", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      //如果返回状态正常，将请求的数据保存在data中
      this.userList = res.data.users;
      this.total = res.data.total;
    },
  },
  created() {
    this.getTableData();
  },
};
</script>

<style lang="less" scoped>
</style>